<template>
	<view class="cg-custom-dialog">
		<cg-modal ref="modal"></cg-modal>
	</view>
</template>

<script>
	/* 
	 * 全局弹窗，只在当前页弹窗,微信小程序中需要写class <cg-custom-alert class="cg-custom-dialog"></cg-custom-alert>
	 */
	import {
		getRouteUrl
	} from '@/common/utils/router.js';
	import { eventBus } from '@/common/utils/eventBus';
	export default {
		name: "cg-custom-alert",
		data() {
			return {
				route: ''
			};
		},
		mounted() {
			console.log('mounted====',eventBus.list()); //
			// #ifdef H5
			this.route = this.$parent.$route.meta.pagePath;
			console.log('this.route',`customAlert${this.route}`);
			eventBus.$on(`customAlert${this.route}`, (route, config) => {
				console.log('在监听：', route);
				/* 不在当前页无需弹窗 */
				// if (route !== this.route) return
				this.$refs.modal.openDialog({
					...config
				})
			})
			// #endif
			// #ifndef H5
			if (this.$parent.$mp && !this.$parent?.$mp?.page) {
				console.error(
					'unknow page, please check,this compnents needs a page as a parent,can not used in another component'
					)
				return
			}
			console.log(222222222222222222222, this.$parent?.$mp?.page.route);
			this.route = this.$parent?.$mp.page.route;
			eventBus.$on(`customAlert${this.route}`, (route, config) => {
				console.log('===========e', route, this.route);
				/* 不在当前页无需弹窗 */
				// if (route !== this.route) return
				this.$refs.modal.openDialog({
					...config
				})
			})
			// #endif
			
		},

		beforeDestroy() {
			eventBus.$off(`customAlert${this.route}`)
			console.log('已销毁监听，避免内存泄漏',);
		}

	}
</script>

<style>

</style>